/* index Page Javascript */
// editter function
var chgfunc=(function(a){
	//check
	if(!a){a={};}
	if(!a.iptw){a.iptw=200;}
	if(!a.el){alert("error : not element : function[chgfunc]");return null;}
	//hover
	a.el.hover((function(){
		$(this).css({'background-color':'#dddddd',cursor:'pointer'});
	}),
	(function(){
		$(this).css({'background-color':'#ffffff',cursor:'default'});
	}));
	//return func
	return (function(){
		// to reverse style
		$(this).trigger('mouseout');
		var f,g,h=this.outerText,i
		,format1='<input type="text" class="editer" style="width:{0}px" value="{1}" />'
		,format2=this.outerHTML.replace('>'+h+'</div>','>{0}</div>');
		// reset event & change tag
		f=$(fc.format(format1,[a.iptw,$(this).text()]));
		$(this).unbind('click').replaceWith(f);
		// set input's event
		var reverse_func=(function(){
			i=jQuery.trim($(this).val());
			g=$(fc.format(format2,[i]));
			a.el=g;
			g.click(chgfunc(a));
			$(this).replaceWith(g);
			if(h!==i){
				g.css({color:'red'});
			}
			if(a.refunc){a.refunc($(this));}
		});
		f.keypress(function(key_value){
			//which code 13 -> Enter key
			if(key_value.which!=13){return null;}
			// reverse div tag mode
			$(this).trigger('blur');
		}).blur(reverse_func).get(0).focus();
		if(a.prefunc){a.prefunc(f);}
	});
});
//extands fitem
(function(){
	// add exe (init)
	fitem.prototype.old_init=fitem.prototype.init;
	fitem.prototype.format='<div class="content" search="correct"><img class="itemimg" src="{0}" onmousedown="return false"/><div class="store editor_div">{1}</div><div class="brand editor_div">{2}</div></div>';
	fitem.prototype.init=(function(a){
		var b=this;
		this.old_init(a);
		//add set event -> parameter
		this.el.find('div').each(function(){
			$(this).click(chgfunc({el:$(this)}));
		});
	});
})();
//kind of tab:0->all,1->info,2->contents
var istab=0;
var fitems={};
//Onload
jQuery(function(){
	//item
	//sample data
	var imgary=new Array();
	for(var i=0;i<30;i++){
		var id_tmp='detail'+(i==0?'':'-'+i)+'.jpeg';
		imgary.push({
			id:id_tmp
			,img:'img/'+id_tmp
			,name:'name'+i
			,brand:'brand'+i
			,store:'store_name'+i
		});
	}
	
	// set image of items
	var tmp=null,tmp2=null;
	$('#contents').hide();
	jQuery.each(imgary,function(){
		//tmp={'id':imgary[i]['id']};
		tmp2=new fitem(this);
		tmp2.appendEl('contents');
		fitems[tmp2.id]=tmp2;
	});
	$('#contents').fadeIn(500);
	
	//editter of item
	$('#myname').click(chgfunc({iptw:400,el:$('#myname')}));
	$('#myurl').click(chgfunc({iptw:400,el:$('#myurl')}));
	
	//item drop system
	//content
	$(document).bind('dragover',function(){
		if(istab==1){return null;}
		$('#dropitem').css({
			 'background-color':'black'
			,display:'block'
			,top:$(this).scrollTop()
		});
	});
	$('#dropfitem').change(function(e){
		fc.$id('dropform').submit();
		$(document).trigger('mouseover');
		fc.$id('search_bar').focus();
		$('#dropitem').css({
			 'background-color':'black'
			,display:'none'
		});
		e.preventDefault();
		e.stopPropagation();
	}).bind('dragleave',function(){
		$('#dropitem').css({
			display:'none'
		});
	});
	//banner
	$('#mybanner').bind('dragover',function(){
		if(istab!=1){return null;}
		$(this).css({
			 'background-color':'#dddddd'
			,display:'block'
			,top:$(this).scrollTop()
		});
	}).bind('dragleave',function(){
		$(this).css({
			 'background-color':'#ffffff'
		});
	});
	
	//search
	var search_exe=(function(){
		var a=$('#search_bar');
		if(!fc.cknull(a.val())){return null;}
		var result=true,dbary=new Array(),jsary=new Array();
		//make search request(DB)
		//sample code
		//make search request(JS)
		var b=new RegExp('('+a.val().replace(' ','|')+')'),c=null;
		for(var d in fitems){
			c=fitems[d];
			if(c.name.match(b)){
				jsary.push(c);
			}
		}
		if(jsary.length <= 0){
			//if not found match item 
			alert('not found');
			return false;
		}
		// all hide item
		$('#contents .content').attr('search','miss').filter(':visible')
		.fadeOut(
			 'normal'
			,function(){
				// show item exe
				for(var e in jsary){jsary[e].el.attr('search','correct')}
				$('#contents .content[search=correct]').fadeIn('normal');
			}
		);
		//request -> DB
		//display clean
	});
	$('#search_btn').click(search_exe);
	$('#search_bar').keypress(function(key_value){
		//which code 13 -> Enter key
		if(key_value.which!=13){return null;}
		search_exe();
	})
	
	// Map system
	var latlng,mymapoptions,map,map_marker=null;
	try{
		//default setting
		latlng= new google.maps.LatLng(35.782171,139.526367);
		mymapoptions={
		   zoom:4
		  ,center:latlng
		  ,mapTypeId:google.maps.MapTypeId.ROADMAP
		  ,disableDoubleClickZoom:false
		  //,draggable:false
		  ,keyboardShortcuts:false
		  ,panControl:false
		  ,overviewMapControl:false
		  ,mapTypeControl:false
		  ,streetViewControl:false
		  ,scrollwheel:false
		};
		map= new google.maps.Map(document.getElementById("mymap"), mymapoptions);
		//if be registered , set param
		if(fc.cknull($('#mymap_x').val())&&fc.cknull($('#mymap_y').val())){
			latlng= new google.maps.LatLng($('#mymap_x').val(),$('#mymap_y').val());
			map.setCenter(latlng);
		    map.setZoom(16);
			map_marker= new google.maps.Marker({
        		map: map,
				position: latlng
			});
		}
	}catch(err){}
	var geocoder = new google.maps.Geocoder();
	$('#mymapgeo').click(chgfunc({
		 iptw:600
		,el:$('#mymapgeo')
		,refunc:function(obj){}
		,prefunc:function(obj){}
	}));
	$('#mymap_search').click(function(){
		var address = $('#mymapgeo').html();
		if(geocoder){
			geocoder.geocode({'address':address},function(results, status) {
		        if(status==google.maps.GeocoderStatus.OK) {
		        	var loc=results[0].geometry.location;
		        	map.setCenter(loc);
		        	map.setZoom(16);
		        	if(map_marker)map_marker.setMap(null);
					map_marker= new google.maps.Marker({
		        		map: map, 
						position: loc
					});
					$('#mymap_x').val(loc.lat().toString());
					$('#mymap_y').val(loc.lng().toString());
		        }else{
		          //not found
				}
			});
	    }
	});
	
	//tab system
	$('#tablist li').hover(
		 function(){
		 	if('selected'===$(this).attr('class'))return null;
		 	$(this).attr('class','hover_on');
		 }
		,function(){
		 	if('selected'===$(this).attr('class'))return null;
		 	$(this).attr('class','');
		}
	).click(function(){
		var ij='hide';
		var ik=$(this).attr('tar');
		if('all'===ik){ij='show';istab=0;}
		else if('sinfo'===ik){istab=1;}
		else if('contents'===ik){istab=2;}
		$(this).siblings().each(function(){
			$('#'+$(this).attr('tar'))[ij]();
			$(this).attr('class','');
		});
		$('#'+ik).show();
		$(this).attr('class','selected');
	});
	$('#tabdefault').trigger('click');
});

// Upload system [child frame -> parent frame]
function upload_file(src){
	var tsrc=fc.getfile(src);
	var a={
			id:tsrc
			,img:'tmp/'+tsrc
			,name:'name'
			,brand:'brand'
			,store:'store_name'};
	tmp2=new fitem(a);
	tmp2.appendEl('contents',true);
	fitems[tmp2.id]=tmp2;
}
